package com.tangyee.home_exercise_diet.activities;

import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangyee.R;
import com.tangyee.home_exercise_diet.beans.FoodOrExercise;
import com.tangyee.home_exercise_diet.beans.TypeEnum;
import com.tangyee.home_exercise_diet.dao.FoodOrExerciseHolder;

import java.util.List;

/**
 * 删除记录的Activity，就一个RecyclerView
 */
public class EditFoodOrExerciseList extends AppCompatActivity {

    public static final String DATE = "date";
    public static final String TYPE = "type";

    private RecyclerView mRecyclerView;
    private MyAdapter adapter;

    private ImageView back;
    private TextView centerTv;
    private TextView advice;
    private TextView subtotal;

    private String date;
    private String type;
    private List<FoodOrExercise> mFoodOrExercises;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_food_or_exercise_list);

        date = getIntent().getStringExtra(DATE);
        type = getIntent().getStringExtra(TYPE);
        mFoodOrExercises = FoodOrExerciseHolder.get(getApplicationContext()).getListByDateAndType(date, type);

        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        centerTv = findViewById(R.id.center_text);
        centerTv.setText(TypeEnum.valueOf(type).getTypeName());
        advice = findViewById(R.id.advice);
        subtotal = findViewById(R.id.subtotal);
        subtotal.setText(String.format("小计：%.2f千卡",getSubtotal()));

        mRecyclerView = findViewById(R.id.edit_records_recyclerview);
        mRecyclerView.setHasFixedSize(true);
        this.mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyAdapter(this, mFoodOrExercises);
        this.mRecyclerView.setAdapter(adapter);
    }

    public double getSubtotal(){
        if (mFoodOrExercises == null || mFoodOrExercises.size() == 0)
            return 0;
        double s = 0;
        int unit = 100;
        if (mFoodOrExercises.get(0).getType().equals(TypeEnum.EXERCISE))
            unit = 60;
        for (FoodOrExercise foodOrExercise : mFoodOrExercises){
            s += foodOrExercise.getAmount()/unit*foodOrExercise.getPerUnitCalorie();
        }
        return s;
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        Context mContext;
        List<FoodOrExercise> mFoodOrExerciseList;

        public MyAdapter(Context context, List<FoodOrExercise> list){
            mContext = context;
            mFoodOrExerciseList = list;
        }

        public void setFoodOrExerciseList(List<FoodOrExercise> foodOrExercises){
            this.mFoodOrExerciseList = foodOrExercises;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.exercise_and_diet_scrollview_item, parent, false);
            view.setBackgroundResource(R.drawable.bottom_line_shape);
            MyHolder myHolder = new MyHolder(view);
            return myHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            final FoodOrExercise foodOrExercise = mFoodOrExerciseList.get(position);
            int n = 100;
            if (foodOrExercise.getType().equals(TypeEnum.EXERCISE)){
                n = 60;
            }

            String unit = foodOrExercise.getUnit();
            unit = unit.substring(unit.lastIndexOf('0')+1);

            final ImageView item_img = ((MyHolder)holder).item_img;
            item_img.setImageResource(R.drawable.delete);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
                item_img.setBackground(null);
            item_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FoodOrExerciseHolder.get(getApplicationContext()).deleteRecord(date, type, foodOrExercise.getName());
                    mFoodOrExercises = FoodOrExerciseHolder.get(getApplicationContext()).getListByDateAndType(date, type);
                    adapter.setFoodOrExerciseList(mFoodOrExercises);
                    adapter.notifyDataSetChanged();
                    subtotal.setText(String.format("小计：%.2f千卡",getSubtotal()));
                }
            });
            ((MyHolder)holder).food_title.setText(foodOrExercise.getName());
            ((MyHolder)holder).food_weight.setText(String.format("%.2f", foodOrExercise.getAmount()) + unit);
            ((MyHolder)holder).food_calorie.setText(String.format("%.2f", foodOrExercise.getPerUnitCalorie()*1.0*foodOrExercise.getAmount()/n)+"千卡");

        }

        @Override
        public int getItemCount() {
            if (mFoodOrExerciseList == null)
                return 0;
            return mFoodOrExerciseList.size();
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {

        ImageView item_img;
        TextView food_title;
        TextView food_weight;
        TextView food_calorie;

        public MyHolder(View itemView) {
            super(itemView);
            item_img = itemView.findViewById(R.id.item_img);
            food_title = itemView.findViewById(R.id.food_title);
            food_weight = itemView.findViewById(R.id.food_weight);
            food_calorie = itemView.findViewById(R.id.food_calorie);
        }
    }

}
